package com.qiantu.phone.widget;

import static com.scwang.smart.refresh.layout.util.SmartUtil.dp2px;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SoundEffectConstants;
import android.view.View;

import com.qiantu.phone.R;
import com.qiantu.phone.app.AppApplication;

public class VerticalStatusSeekBar extends View {
    private float progress;
    private float progressTextSize;
    private float progressTrackHeight;
    private float mThumbWidth;
    private float mThumbStrokeHeight;
    private float mThumbHeight;
    private float thumbCy;
    private float canSlideTotalHeight;
    private int bottomHeight;
    private int mThumbDragOffset;
    private int[] gradientColors;
    private boolean isFirst;

    private boolean isOnlyClick = false;
    private LinearGradient mLinearGradient;
    // 画笔
    private Paint mPaint;
    private Paint mTextPaint;
    private Paint mThumbPaint;
    private RectF mBarRectF;
    private RectF mThumbRectF;
    private boolean isNotSet;
    private VerticalStatusSeekBar.OnSeekBarChangeListener mOnSeekBarChangeListener;
    public VerticalStatusSeekBar(Context context) {
        this(context, null);
        init();
    }

    public VerticalStatusSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mBarRectF = new RectF();
        progressTrackHeight = dp2px(16);
        mThumbStrokeHeight = dp2px(2);
        mThumbWidth = dp2px(24);
        mThumbHeight = dp2px(8);
        mThumbDragOffset = dp2px(10);
        bottomHeight = dp2px(36);
        progressTextSize = dp2px(16);

        mPaint = new Paint();
        mPaint.setAntiAlias(true);

        mThumbPaint = new Paint();
        mThumbPaint.setStrokeWidth(mThumbStrokeHeight);
        mThumbPaint.setAntiAlias(true);

        mTextPaint = new TextPaint();
        mTextPaint.setAntiAlias(true);

        gradientColors = new int[]{AppApplication.getInstance().getThemeColor(R.attr.themeColor), AppApplication.getInstance().getThemeColor(R.attr.themeColor)};

    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int dw = getMeasuredWidth();
        int dh = getMeasuredHeight();
        setMeasuredDimension(dw, dh);
        mBarRectF.left = dw/2 - progressTrackHeight/2;
        mBarRectF.right = mBarRectF.left + progressTrackHeight;
        mBarRectF.top = 0;
        mBarRectF.bottom = dh - bottomHeight;
        mLinearGradient = new LinearGradient(0, 0, dw, 0, gradientColors, null, Shader.TileMode.CLAMP);
        canSlideTotalHeight = mBarRectF.height() - 2 * mThumbDragOffset;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setShader(mLinearGradient);
        canvas.drawRoundRect(mBarRectF, progressTrackHeight, progressTrackHeight, mPaint);
        mPaint.setShader(null);

        thumbCy = mThumbDragOffset + canSlideTotalHeight * progress;
        mThumbRectF = new RectF();
        mThumbPaint.setColor(0xffffffff);
        mThumbPaint.setStyle(Paint.Style.FILL);
        mThumbRectF.left =mThumbStrokeHeight/2;
        mThumbRectF.top = thumbCy - mThumbHeight/2;
        mThumbRectF.right = getMeasuredWidth() - mThumbStrokeHeight/2;
        mThumbRectF.bottom = mThumbRectF.top + mThumbHeight;
        canvas.drawRoundRect(mThumbRectF, mThumbWidth, mThumbWidth, mThumbPaint);
        mThumbPaint.setColor(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
        mThumbPaint.setStyle(Paint.Style.STROKE);
        canvas.drawRoundRect(mThumbRectF, mThumbWidth, mThumbWidth, mThumbPaint);

        String progressText = null;
        if(isNotSet){
            progressText = getResources().getString(R.string.not_set);
        }else{
            progressText = (int)(180 * progress) + "°";
        }
        mTextPaint.setTextSize(progressTextSize);
        mTextPaint.setColor(AppApplication.getInstance().getThemeColor(R.attr.secondaryColor6));
        canvas.drawText(progressText, 0, getHeight() - mTextPaint.getFontMetrics().bottom, mTextPaint);
    }

    private void drawBackground(Canvas canvas) {

    }

    private void drawCircle(Canvas canvas) {
//
    }

    float indicatorOffset = 0;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                isFirst = true;
                //只有Thumb区域可以点击拖动
//                if (event.getY() >= mThumbRectF.top && event.getY() <= mThumbRectF.bottom) {
//                    //圆点之内可以拖动
//                    isOnlyClick = false;
//                } else {
//                    //圆点之外只能点击
//                    isOnlyClick = true;
//                }
                break;
            case MotionEvent.ACTION_MOVE:
                final float y = event.getY();
//                if (!isOnlyClick) {
                    if (isFirst) {
                        isFirst = false;
//                        setAlpha(0.8f);
                        if (mOnSeekBarChangeListener != null) {
                            mOnSeekBarChangeListener.onStartTrackingTouch();
                        }
                    }
                    getParent().requestDisallowInterceptTouchEvent(true);
                    progress = (y - mThumbDragOffset)/canSlideTotalHeight;
                    progress = progress < 0 ? 0 : progress;
                    progress = progress > 1 ? 1 : progress;
//                    calculateTemp();
                    invalidate();
                    if (mOnSeekBarChangeListener != null) {
                        mOnSeekBarChangeListener.onValueChanged((int)(180 * progress));
                    }
//                }
                break;
            case MotionEvent.ACTION_UP:
                //点击
//                float upX = event.getX();
//                float upY = event.getY();
//                float clickDistance = (float) Math.sqrt(Math.pow(Math.abs(upX - mTouchDownX), 2) + Math.pow(Math.abs(upY - mTouchDownY), 2));
//                if (clickDistance <= dp2px(2)) {
//                    progress = (event.getX() - mThumbLeftRightOffset) / canSlideTotalWidth;
//                    progress = progress < 0 ? 0 : progress;
//                    progress = progress >= 0.995 ? 1 : progress;
//                    Log.i("luzx", "progress" + progress);
//                    calculateTemp();
//                    invalidate();
//                }
                getParent().requestDisallowInterceptTouchEvent(false);
                isNotSet = false;
                if (mOnSeekBarChangeListener != null) {
                    playSoundEffect(SoundEffectConstants.CLICK);
                    mOnSeekBarChangeListener.onStopTrackingTouch((int)(180 * progress));
                }
                break;
        }
        return true;
    }


    public interface OnSeekBarChangeListener {
        /**
         * 进度监听回调
         *
         * @param value
         */
        public void onValueChanged(int value);

        /**
         * 开始拖动
         */
        public void onStartTrackingTouch();

        /**
         * 停止拖动
         */
        public void onStopTrackingTouch(int value);
    }

    public void setOnSeekBarChangeListener(VerticalStatusSeekBar.OnSeekBarChangeListener changeListener) {
        this.mOnSeekBarChangeListener = changeListener;
    }

    public void setValue(int temp) {
        progress = temp / 180f;
        progress = progress < 0 ? 0 : progress;
        progress = progress > 1 ? 1 : progress;
        isNotSet = false;
        invalidate();
    }

    public void isNotSet(boolean isNotSet) {
        this.isNotSet = isNotSet;
        progress = 0;
        invalidate();
    }

    public int dp2px(float dp) {
        return (int) (getResources().getDimension(R.dimen.dp_1) * dp);
    }
}
